<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"
 integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"
 integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd"
 crossorigin="anonymous"></script>
</head>
<body>
    <style>
        *{
            margin: 0;padding: 0;
        }
        .box{
            width: 1000px;
            height: 600px;
            margin: auto;
            background-color: rgba(215, 208, 208, 0.6);
        }
        .box h3{
            text-align: center;
            background-color: rgba(209, 207, 207, 0.6);
            display: block;
            width: 300px;
            margin-left: 35%;
            border-bottom-left-radius: 30px;
            border-bottom-right-radius: 30px;
        }
        .box h5{
            text-align: center;
            background-color: rgba(162, 158, 158, 0.6);
            display: block;
            width: 200px;
            margin-left: 40%;
            margin-top: 20px;
            
        }
        .se{
            width: 200px;
            margin-left: 40%;
        }
        .zw,#gm{
            width: 100px;
           margin-left: 45%;
           margin-top: 10px;
        }
        table{
            margin: auto;
            background-color: white;
        }
        table td{
            width: 50px;
            height: 50px;
        }
        .green{
            background-color: green;
        }
        .red{
            background-color: red;
        }
    </style>

   <div class="box">
     <h3>第六号放映厅</h3>
      <h5>银幕中央</h5>
      <input type="text" class="form-control se" id="hang" placeholder="请输入行">
      <input type="text" class="form-control se" id="lie" placeholder="请输入列">
      <button class="btn btn-default zw" onclick="tj()" type="submit" id="addzw">添加座位</button>
      <div class="box2">
         
             

      </div>
      <a class="btn btn-default" href="#" role="button" id="gm">确认购买</a>
    </div>
  
    
</body>
<script>

   var seat=[]
   $(function(){

    showSeatList()
    $('td').click(bianse)
    $('#gm').click(gm)
   })

   // 添加座位
    function tj(){
     var hang = $('#hang').val()
     var lie = $('#lie').val()
     var str =''
          str += ` <table class="table table-hover" border="1">`  
            for (var i =0;i<hang;i++){
                str +=`<tr>`
                    for(var j = 0; j<lie;j++){
                        str +=`<td></td>`
                    }
                    str += `</tr>`
            }
            str += `</table>` 
            seat.push(str)
    localStorage.setItem('seat',JSON.stringify(seat))
    showSeatList()
    }
    // 显示座位
    function showSeatList(){
        seat = localStorage.getItem('seat')
           ? JSON.parse(localStorage.getItem('seat'))
           :[]
    var strHtml =''
     for (let i in seat) {
         strHtml += `${seat}
         `
     }
     $('.box2').html(strHtml)
   
    }
    // 变绿
    function bianse(){
        console.log(12);
          $(this).addClass('green');
          console.log($(this).index()); 
    }
    

   // 本地存储
    function getListData(key, data = null, isPush = false) {
        //   如果本地存储有就获取   没有就返回空[]
        let localData = localStorage.getItem(key) ? JSON.parse(localStorage.getItem(key)) : []
        if (data) {
            //   如果是  true 就添加数据
            if (isPush) {
                //  设置本地存储
                localData.push(data)
                localStorage.setItem(key, JSON.stringify(localData)) //  设置本地存储
            } else {
                //   如果是false 就覆盖数据
                localStorage.setItem(key, JSON.stringify(data))
            }
        }
        return localData
    }
</script>
</html>